package com.c6906.vdam.dcs.pool;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.producer.Producer;

public class ProducerPool {
    private final GenericObjectPool<Producer> pool;

    public ProducerPool(String endpoint, String topic) throws ClientException {
        ProducerFactory factory = new ProducerFactory(endpoint, topic);
        GenericObjectPoolConfig<Producer> config = new GenericObjectPoolConfig<>();
        config.setMaxTotal(30); // 最大连接数
        config.setMaxIdle(10); // 最大空闲连接数
        config.setMinIdle(5); // 最小空闲连接数
        this.pool = new GenericObjectPool<>(factory, config);
    }

    public Producer borrowProducer() throws Exception {
        return pool.borrowObject();
    }

    public void returnProducer(Producer producer) {
        pool.returnObject(producer);
    }

    public void close() {
        pool.close();
    }
}

